﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using XiaoMan.BLL;
using XiaoMan.Model.Models;
using XiaoMan.Service;

namespace XiaoMan.API.Controllers
{
    [RoutePrefix("api/disease")]
    public class DiseaseController : ApiBaseController
    {
        Sys_DiseaseService sds = new Sys_DiseaseService();
        Sys_UserAndDiseaseService suds = new Sys_UserAndDiseaseService();
        Sys_UsersService sus = new Sys_UsersService();

        /// <summary>
        /// 根据用户ID查询疾病圈子，并标记好已关注和未关注
        /// </summary>
        /// <returns></returns>
        [Route("getfollowdisease")]
        [HttpGet]
        public IHttpActionResult getFollowDisease()
        {
            int userid = Convert.ToInt32(Request.Headers.GetValues("userid").FirstOrDefault());

            var DiseaseOnUser = sds.GetList().Select(d=>d.ToModel()).ToList();
            Sys_Users usersInfo = sus.GetUserByID(userid);
            if (usersInfo != null)
            {
                //拿到所有关注的人ID
                List<Sys_UserAndDisease> udInfoList = suds.GetListByUserID(userid);
                if (udInfoList != null && udInfoList.Count > 0)
                {
                    foreach (Sys_UserAndDisease udInfo in udInfoList)
                    {
                        for (int i = 0; i < DiseaseOnUser.Count(); i++)
                        {
                            if (udInfo.DiseaseID.Value==DiseaseOnUser[i].DiseaseID)
                            {
                                DiseaseOnUser[i].IsFollow = "1";
                                break;
                            }
                        }
                        
                    }
                }
                return ApiSuccess(DiseaseOnUser);
            }
            return ApiFail("用户不存在！");
        }
        //public IHttpActionResult getNoFollowDisease()
        //{
        //    int userid = Convert.ToInt32(Request.Headers.GetValues("userid").FirstOrDefault());

        //    Sys_Users usersInfo = sus.GetUserByID(userid);
        //    if (usersInfo != null)
        //    {
        //        //拿到所有关注的人ID
        //        List<int> diseaseList = new List<int>();
        //        List<Sys_UserAndDisease> udInfoList = suds.GetListByUserID(userid);
        //        if (udInfoList != null && udInfoList.Count > 0)
        //        {
        //            foreach (Sys_UserAndDisease udInfo in udInfoList)
        //            {
        //                if (!diseaseList.Contains(udInfo.DiseaseID.Value))
        //                {
        //                    diseaseList.Add(udInfo.DiseaseID.Value);
        //                }
        //            }
        //        }
        //        List<Sys_Disease> diseaseInfoList = null;
        //        if (diseaseList.Count > 0)
        //        {
        //            diseaseInfoList = sds.GetNotExistList(diseaseList);
        //        }
        //        else
        //        {
        //            udInfoList = new List<Sys_UserAndDisease>();
        //            diseaseInfoList = sds.GetList();
        //        }
        //        if (diseaseInfoList != null && diseaseInfoList.Count > 0)
        //        {
        //            Sys_UserAndDisease userAndDiseaseInfo = new Sys_UserAndDisease();
        //            foreach (Sys_Disease diseaseInfo in diseaseInfoList)
        //            {
        //                userAndDiseaseInfo.DiseaseID = diseaseInfo.DiseaseID;
        //                userAndDiseaseInfo.DiseaseName = diseaseInfo.DiseaseName;
        //                userAndDiseaseInfo.UserAndDiseaseID = 0;//返回的未关注的此字段的值为0，关注的则不为0
        //                userAndDiseaseInfo.UserID = userid;
        //                userAndDiseaseInfo.UserNikeName = usersInfo.NickName;
        //                udInfoList.Add(userAndDiseaseInfo);
        //            }
        //        }

        //        return ApiSuccess(udInfoList);
        //    }
        //    return ApiFail("用户不存在！");
        //}
    }
}